<?php

namespace app\api\controller\app;

use app\admin\model\Banner;
use app\admin\model\City;
use app\admin\model\Org;
use app\admin\model\OrgIndexNotice;
use app\admin\model\OrgNotice;
use app\admin\model\Roomclean;
use app\admin\model\Shop;
use app\admin\model\Shopdevice;
use app\admin\model\Shopmno;
use app\admin\model\Shoporder;
use app\admin\model\Shoporderprofit;
use app\admin\model\Shoproom;
use app\admin\model\Shoproombook;
use app\admin\model\Userbalance;
use app\admin\model\Userorderprofit;
use app\admin\model\Usertuan;
use app\api\service\DeviceService;
use app\api\service\KsService;
use app\api\service\MtService;
use app\api\service\MtUtil;
use app\api\service\VbillService;
use app\common\controller\Api;
use app\common\library\Ems;
use app\common\library\Sms;
use fast\Http;
use fast\Random;
use think\Config;
use think\Validate;

/**
 */
class Ks extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = '*';

    protected $appKey = "ks661184720738998777";
    protected $appSecret = "Eb_O_GneEhRTgfFHSx8Bsg";
    protected $signSecret = "850bafe071fde0752d8c1411d071135a";
    protected $msgKey = "JnY3KV/fELpsGpM7VJ4G1A==";

    protected $get_access_token = "https://lbs-open.kuaishou.com/oauth2/access_token";

    public function _initialize()
    {
        parent::_initialize();

    }

    public function testProduct()
    {


        $merchantId = "210000580250994324";
        $cache = $this->longRedis->get($merchantId);

        if($cache == ''){
            $this->error("快手未授权");
        }
        $cache = json_decode($cache,true);
        $access_token = $cache['access_token'];
        $expires_in = $cache['expires_in'];
        if($expires_in <strtotime("-1 minute")){
            $ksService = new KsService();
            $p = array();
            $p['ks_id'] = $merchantId;
            $access_token = $ksService->refresh($this->longRedis,$p);
        }
        $url = "https://lbs-open.kuaishou.com/goodlife/v1/item/itemlist/batch/query";


        $req = array();
        $req['cursor'] = 0;
        $req['size'] = 50;
        $req['start_time'] = 1704682034618;
        $req['end_time'] = 1723085234618;


        $result = KsService::post($url,json_encode($req),$access_token);

        $this->success("",json_decode($result));

    }

    public function callback()
    {
        $param = file_get_contents("php://input");

        $code = $this->request->param('code');
        $state = $this->request->param('state');

        $userId =  explode("-",$state)[0];
        $merchantId =  explode("-",$state)[1];

        error_log($code."====ks callback===".$state);

        $req = array();
        $req['app_id'] = $this->appKey;
        $req['grant_type'] = "code";
        $req['code'] = $code;
        $req['app_secret'] = $this->appSecret;

        $result = Http::get($this->get_access_token,$req);

        error_log("==ks=auth==".$result);
        $result = json_decode($result,true);
        $code = $result['result'];
        if($code == 1){

            $access_token = $result['access_token'];
            $refresh_token = $result['refresh_token'];

            $shopModel = new Shop();
            $shop = $shopModel->where('ks_id',$merchantId)->find();
            $cache = array();
            $cache['access_token'] = $access_token;
            $cache['expires_in'] = strtotime("+48 hour");

            $this->longRedis->set($merchantId,json_encode($cache));
            if($shop){
                $shopModel->where('id',$shop['id'])->update(['ks_refresh_token'=>$refresh_token]);
            }
        }else{
            error_log("======快手授权失败======".json_encode($result));
        }

    }


}
